/*
 *******************************************************************
 *
 * COPYRIGHT (coffee) 2013 - 2014 LTV Co., Ltd  All rights reserved
 *
 * Created  : Mar 11, 2014
 * Change History
 * =================================================================
 * DATE                DEVELOPER        REVISION      DESCRIPTION
 * =================================================================
 * Mar 11, 2014        ducnguyen        1.0           First Created.
 * =================================================================
 *
 *
 *******************************************************************/
package vn.com.ltv.lo;

import java.util.ArrayList;

import vn.com.ltv.lo.common.ConfigsAndroid;
import vn.com.ltv.lo.common.Constants;
import vn.com.ltv.lo.daos.CategoryDAO;
import vn.com.ltv.lo.daos.DishDAO;
import vn.com.ltv.lo.daos.ImageDAO;
import vn.com.ltv.lo.daos.SetDAO;
import vn.com.ltv.lo.dtos.CategoryDTO;
import vn.com.ltv.lo.dtos.DishDTO;
import vn.com.ltv.lo.dtos.SetDTO;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.three20.ui.TTImageView;

public class LogoFragment extends Fragment{

	GridView gridView;
	private static final String CATEGORY = "category";
	static int _idCategory = 0;
	ArrayList<SetDTO> foods = new ArrayList<SetDTO>();
	ArrayList<DishDTO> dishDTOs = new ArrayList<DishDTO>();
	public static LogoFragment newInstance(int idCategory) {
		LogoFragment hf = new LogoFragment();	
		Bundle arg = new Bundle();
		arg.putInt(CATEGORY, idCategory);		
		hf.setArguments(arg);
		return hf;  
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.logo, container, false);
		gridView = (GridView) view.findViewById(R.id.gridView1);
		CategoryDAO categoryDAO = new CategoryDAO(getActivity());
		_idCategory = getArguments().getInt(CATEGORY);
		CategoryDTO categoryDTO = categoryDAO.get(_idCategory);
		if (categoryDTO!=null) {
			if (categoryDTO.getCtgType().trim().equals("Set")) {
				SetDAO setDAO = new SetDAO(getActivity());
				foods = setDAO.getAllByCategoryId(_idCategory);
				gridView.setAdapter(new ListFoodAdapter(getActivity(), foods));
			}else{
				DishDAO dishDAO = new DishDAO(getActivity());
				dishDTOs = dishDAO.getAllByCategoryId(_idCategory);
				gridView.setAdapter(new ListDishAdapter(getActivity(), dishDTOs));
			}
		}
		
		
		
		
		return view;
	}
	
	
	public class ListFoodAdapter extends BaseAdapter{

		Context context;
		ArrayList<SetDTO> categories = new ArrayList<SetDTO>();
		LayoutInflater infalInflater;
		FoodHolder catListHolder;
		
		public ListFoodAdapter(Context context, ArrayList<SetDTO> categories){
			this.context = context;
			this.categories = categories;
			infalInflater = (LayoutInflater) this.context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public int getCount() {
			return categories.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			TextView textView = null;
			TTImageView imageView = null;
			TextView textViewPrice;
			RatingBar ratingBar;
			FrameLayout view;
			
			if (convertView == null) {
				catListHolder = new FoodHolder();
				convertView = infalInflater.inflate(
						R.layout.item_gridview_food, null);
				textView = (TextView) convertView
						.findViewById(R.id.textview_name);
				imageView = (TTImageView) convertView.findViewById(R.id.image_food);
				ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar1);
				textViewPrice = (TextView) convertView.findViewById(R.id.textview_price);
				view = (FrameLayout) convertView.findViewById(R.id.view);
				catListHolder.textViewName = textView;
				catListHolder.imageViewBookMark = imageView;
				catListHolder.ratingBar = ratingBar;
				catListHolder.textViewPrice = textViewPrice;
				catListHolder.view = view;
				convertView.setTag(catListHolder);

			} else {
				catListHolder = (FoodHolder) convertView.getTag();
				textView = catListHolder.textViewName;
				imageView = catListHolder.imageViewBookMark;
				ratingBar = catListHolder.ratingBar;
				textViewPrice = catListHolder.textViewPrice;
				view = catListHolder.view;
			}
			
			textView.setText(categories.get(position).getSetNm());
			
			if (ConfigsAndroid.isNetworkAvailable(getActivity())) {
				ImageDAO imageDAO = new ImageDAO(getActivity());
				
				imageView.setImageURL(imageDAO.getPathImage(Integer.parseInt(categories.get(position).getSetIdImg())));
				
			}
			
			ratingBar.setRating(categories.get(position).getSetRtAvrg());
			
			textViewPrice.setText(categories.get(position).getSetPrc()+" VND");
			
			convertView.setOnClickListener(new View.OnClickListener() {
				int p = position;
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(), DetailActivity.class);
					intent.putExtra(Constants.EXTRA_ID, categories.get(p).getSetId());
					intent.putExtra(Constants.EXTRA_TYPE, Constants.EXTRA_VALUE_SET);
					startActivity(intent);
					
				}
			});
			
			
			return convertView;
		}
		
		public class FoodHolder {
			TextView textViewName;
			TTImageView imageViewBookMark;
			TextView textViewPrice;
			RatingBar ratingBar;
			FrameLayout view;
		}
	}
	
	
	public class ListDishAdapter extends BaseAdapter{

		Context context;
		ArrayList<DishDTO> categories = new ArrayList<DishDTO>();
		LayoutInflater infalInflater;
		FoodHolder catListHolder;
		
		public ListDishAdapter(Context context, ArrayList<DishDTO> categories){
			this.context = context;
			this.categories = categories;
			infalInflater = (LayoutInflater) this.context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public int getCount() {
			return categories.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			TextView textView = null;
			TTImageView imageView = null;
			TextView textViewPrice;
			RatingBar ratingBar;
			FrameLayout view;
			
			if (convertView == null) {
				catListHolder = new FoodHolder();
				convertView = infalInflater.inflate(
						R.layout.item_gridview_food, null);
				textView = (TextView) convertView
						.findViewById(R.id.textview_name);
				imageView = (TTImageView) convertView.findViewById(R.id.image_food);
				ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar1);
				textViewPrice = (TextView) convertView.findViewById(R.id.textview_price);
				view = (FrameLayout) convertView.findViewById(R.id.view);
				catListHolder.textViewName = textView;
				catListHolder.imageViewBookMark = imageView;
				catListHolder.ratingBar = ratingBar;
				catListHolder.textViewPrice = textViewPrice;
				catListHolder.view = view;
				convertView.setTag(catListHolder);

			} else {
				catListHolder = (FoodHolder) convertView.getTag();
				textView = catListHolder.textViewName;
				imageView = catListHolder.imageViewBookMark;
				ratingBar = catListHolder.ratingBar;
				textViewPrice = catListHolder.textViewPrice;
				view = catListHolder.view;
			}
			
			textView.setText(categories.get(position).getDshNm());
			
			if (ConfigsAndroid.isNetworkAvailable(getActivity())) {
				imageView.setImageURL(categories.get(position).getDshThumb().trim());
			}
			
			ratingBar.setRating(categories.get(position).getDshRtAvrg());
			
			textViewPrice.setText(categories.get(position).getDshPrc()+" VND");
			
			convertView.setOnClickListener(new View.OnClickListener() {
				int p = position;
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(), DetailActivity.class);
					intent.putExtra(Constants.EXTRA_ID, categories.get(p).getDshId());
					intent.putExtra(Constants.EXTRA_TYPE, Constants.EXTRA_VALUE_DISH);
					startActivity(intent);
					
				}
			});
			return convertView;
		}
		
		public class FoodHolder {
			TextView textViewName;
			TTImageView imageViewBookMark;
			TextView textViewPrice;
			RatingBar ratingBar;
			FrameLayout view;
		}
	}
}

